package com.gimi.cloud.jimi.app.consumer.service.home.impl;

import com.gimi.cloud.jimi.app.consumer.feign.HomePageFeignService;
import com.gimi.cloud.jimi.app.consumer.service.home.HomePageService;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.app.ColumnProdDTO;
import com.gimi.cloud.jimi.dto.base.BaseDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by shazhiwei on 2017/12/27.
 */
@Service
public class HomePageServiceImpl implements HomePageService{
    private static Logger logger = LoggerFactory.getLogger(HomePageServiceImpl.class);
    private final HomePageFeignService homePageFeignService;

    public HomePageServiceImpl(HomePageFeignService homePageFeignService){
        this.homePageFeignService = homePageFeignService;
    }

    @Override
    public void columnList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        long start = System.currentTimeMillis();
        BaseDTO params = JsonUtil.jsonToBean(requestBody,BaseDTO.class);
        Util.sendResponse(homePageFeignService.columnList(params), response);
        logger.info(String.format("===>耗时：%d ms",System.currentTimeMillis() - start));
    }

    @Override
    public void adList(String requestBody, HttpServletRequest request, HttpServletResponse response) throws Exception {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        long start = System.currentTimeMillis();
        BaseDTO params = JsonUtil.jsonToBean(requestBody,BaseDTO.class);
        Util.sendResponse(homePageFeignService.adList(params), response);
        logger.info(String.format("===>耗时：%d ms",System.currentTimeMillis() - start));
    }

	@Override
	public void columnProdList(String requestBody, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        long start = System.currentTimeMillis();
		ColumnProdDTO params = JsonUtil.jsonToBean(requestBody,ColumnProdDTO.class);
        EntityVerification.checkByGroupsThrowsException(params,ColumnProdDTO.ColumnProdList.class);
		Util.sendResponse(homePageFeignService.columnProdList(params), response);
        logger.info(String.format("===>耗时：%d ms",System.currentTimeMillis() - start));
	}

}
